package com.cfjjc.gz.usraccount.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.cfjjc.gz.common.action.PagedAction;
import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.consts.TypeValueConst;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.util.CreateEmailInfoUtil;
import com.cfjjc.gz.common.util.StringUtil;
import com.cfjjc.gz.common.util.TreeConfig;
import com.cfjjc.gz.usraccount.service.A_ORGService;
import com.cfjjc.gz.usraccount.service.A_ORG_BONUSService;
import com.cfjjc.gz.usraccount.vo.A_ORGVO;
import com.cfjjc.gz.usraccount.vo.A_ORG_BONUSVO;
import com.cfjjc.gz.usraccount.vo.A_USRVO;
import java.util.logging.Level;

/**
 * 
 * 说明：对 机构信息表 进行增删改查的前端处理类
 * 
 * @author  liuxun
 * @since 2012-03-06
 * @version 1.0
 */
public class A_ORGAction extends PagedAction  {
	
	private static final Logger logger = Logger.getLogger(A_ORGAction.class);

    private A_ORGVO model = new A_ORGVO();

	private TreeConfig tree = new TreeConfig();
	    private List r_List = new ArrayList();
            private List r_RList= new ArrayList();
 //           private double a=0.20,b=0.30,c=0.42,d =0.47;//直接积分奖励
            private double b=0.30,c=0.42,d =0.47;//直接积分奖励
            
//            private double e=0.10,f=0.12,g=0.05;//间接积分奖励
            
            private double f=0.12,g=0.05;//间接积分奖励
            
//            private double k=0.04,l=0.05,m=0.02,n=0.03;// 补助积分
            
            private double p=0.16;
            private double A=2000;//分享积分
            
 
            
            
    protected int backupMessage;
	

	public int getBackupMessage() {
		return backupMessage;
	}

	public void setBackupMessage(int backupMessage) {
		this.backupMessage = backupMessage;
	}

    @Autowired
    private A_ORGService a_ORGService;
    @Autowired
    private A_ORG_BONUSService a_ORG_BONUSService;

    public A_ORGAction() {
        super();
		this.setQUERY_NAME("browseA_ORGList");
    }

    // 显示机构信息表管理首页
    public String showIndex() throws Exception {
        return INDEX;
    }
    
    // 显示机构信息表管理首页
    public String showIndexView() throws Exception {
        return "index1";
    }
    
    public String showIndex3() throws Exception {
        return "index3";
    }
    
	private A_ORGVO reportmodel1 = new A_ORGVO();
	
	// 报表导出备份
	public String getAllReport() throws Exception {
		try {
			System.out.println("*********getAllReport begin***********");
			rows = a_ORGService.queryA_ORGList(reportmodel1);
			jasperFilePath = "/jasper/usr_pwd_report.jasper";
			
			exportExcel(jasperFilePath, "用户数据.xls", rows);
			
			System.out.println("*********getAllReport end***********");
		}catch (Exception e) {
				e.printStackTrace();
			return ERROR;
		}
		return OPE_INFO;
	}

    // 显示机构信息表列表页面
    public String queryList() throws Exception {
        preparePagedList(QUERY_NAME, getModel());
        if (!pagedRecords.exists()) {
            try {
                a_ORGService.queryA_ORGs((A_ORGVO) pagedRecords.getParam(), pagedRecords);
            } catch (OTSException e) {
                this.addActionError(e.getMessage());
				return ERROR;
            }
        }
        return LIST;
    }

    // 显示树
	public String showTree() throws Exception {
		
		logger.debug("*********showTree*****");
	   
		if(model.getOrg_status()!=null){
			System.out.println("*********showTree   tree.setUrl ******* and getOrg_status:"+model.getOrg_status());
			logger.debug("*********showTree   tree.setUrl ******* and getOrg_status:"+model.getOrg_status());
			tree.setUrl("A_ORG!queryTreeNode.do?model.org_status=" + model.getOrg_status());
		}else{
			System.out.println("*********showTree  model.getOrg_status() is null************");
			tree.setUrl("A_ORG!queryTreeNode.do");
		}
		return TREE;
	}

	// 显示机构信息中的树
	public String queryTreeNode() throws Exception {
		
		logger.debug("*********queryTreeNode***********");

		Map root = null;

		// 显示一级节点时，自动添加根节点
		if (id == null || id.length == 0) {
			A_ORGVO query_root = new A_ORGVO();
			query_root.setOrg_code("1002");
			query_root.setOrg_status(TypeValueConst.ORG_STATUS_NORMAL);
			A_ORGVO res_root = a_ORGService.queryA_ORG(query_root);
			
			root = new HashMap();
			root.put("id", res_root.getOrg_code());
			root.put("text", res_root.getOrg_name());
			root.put("attributes", res_root);
			
			model.setUpper_org_code("1002");
		} else {
			model.setUpper_org_code(id[0]);
		}

		List<A_ORGVO> nodes = null;

		try {
			nodes = a_ORGService.queryA_ORGList(model);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}

		List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
		for (A_ORGVO node : nodes) {
			Map<String, Object> item = new HashMap<String, Object>();

			item.put("id", node.getOrg_code());
			item.put("text", node.getOrg_code() + " " + node.getOrg_name());
			item.put("attributes", node);
			if ("0".equals(node.getIs_leaf())) {
				item.put("state", "closed");
			}
			items.add(item);
		}

		if (root != null) {
			root.put("children", items);
			this.rows.add(root);
		} else {
			this.rows = items;
		}

		return TREE_NODE;
	}
	
    // 显示机构信息表的明细页面
	public String showInfo() throws Exception {
		
		logger.debug("*********showInfo**********");
		
		try {
			
			logger.debug("*********model.getKey()**********"+model.getKey());
			if (StringUtil.isEmpty(model.getKey())) {
				return INFO;
			}

			A_ORGVO param=new A_ORGVO();
			
			logger.debug("*********showInfo  model.getKey()**********"+model.getKey());
			
        	param.setKey(model.getKey());
			model=a_ORGService.queryA_ORG(param);
		} catch (OTSException e) {
			this.addActionError(e.getMessage());
			return ERROR_PAGE;
		}
		return INFO;
	}

    // 显示机构信息表录入页面
    public String showAdd() throws Exception {

    	try {
	    	if (StringUtil.isNotEmpty(model.getOrg_code())) {
				A_ORGVO parentRes = a_ORGService.queryA_ORG(model);
				if (parentRes == null) {
					addActionError("要操作的记录不存在,请刷新页面后重试或联系管理员!");
					return ERROR_PAGE;
				}
				model.setUpper_org_code(parentRes.getOrg_code());
				model.setOrg_sequence(parentRes.getOrg_sequence());
			} else {
			}
			model.setOrg_code(null);
			model.setIs_leaf("1");
		} catch (OTSException e) {
			addActionError(e.getMessage());
			return ERROR_PAGE;
		}
        setOperationType(SystemConst.OPERATION_TYPE_NEW);
        return INPUT;
    }
    
    public String showAdd1() throws Exception {
    	
    	setOperationType(SystemConst.OPERATION_TYPE_NEW);
        return "input1";
    }
    

    // 新入会员的保存与计算
    public String doInsert() throws Exception {
        try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setCreate_by(usr.getUsr_id());
        	model.setMembertype("1");
        	model.setProduct("白金8888套餐");
        /*	model.setYxx("1");*/
    //1.保存基础信息。   	
            a_ORGService.doInsertA_ORG(model);
            A_ORG_BONUSVO bonus= new A_ORG_BONUSVO();
            bonus.setOrg_code(model.getOrg_code());
            bonus.setOrg_name(model.getOrg_name());
            bonus.setMobile(model.getMobile());
            bonus.setIfclear("0");
            bonus.setLast_extraction_bonus("0");
            bonus.setLastlast_extraction_bonus("0");
            a_ORG_BONUSService.doInsertA_ORG(bonus);
    //2.计算整条链路的会员状态的发生状况。
          dealTreeORG(model);  
    //3.计算分钱数     
          if(r_List.size()>1)
          {
                dealScore();    
          }
      
    //测试备份报表
			/*System.out.println("*********getAllReport begin***********");
			rows = a_ORGService.queryA_ORGList(reportmodel1);
 
			System.out.println("rows size:"+rows.size());
			
			A_ORGVO testVo;
			
			List listdata = new ArrayList();
			
			List listdatachild;
			
			for(int j=0;j<rows.size();j++)
			{   
				testVo = new A_ORGVO();
				
				listdatachild = new ArrayList();
				
				testVo = (A_ORGVO) rows.get(j);
				if(j==0)
				{
					listdatachild.add("Org_id");
					listdatachild.add("Org_code");
					listdatachild.add("Org_name");
					listdatachild.add("Upper_org_code");
					listdatachild.add("Is_leaf");
					listdatachild.add("Org_desc");
					listdatachild.add("Org_order");
					listdatachild.add("Is_hide");
					listdatachild.add("Org_status");
					listdatachild.add("Create_by");
					listdatachild.add("Create_at");
					listdatachild.add("Update_by");
					listdatachild.add("Update_at");
					
					listdatachild.add("Mobile");
					listdatachild.add("Address");
					listdatachild.add("Banknumber");
					listdatachild.add("Banktype");
					listdatachild.add("Membertype");
					listdatachild.add("Product");	
				}
				else{
				listdatachild.add(testVo.getOrg_id());
				listdatachild.add(testVo.getOrg_code());
				listdatachild.add(testVo.getOrg_name());
				listdatachild.add(testVo.getUpper_org_code());
				listdatachild.add(testVo.getIs_leaf());
				listdatachild.add(testVo.getOrg_desc());
				listdatachild.add(testVo.getOrg_order());
				listdatachild.add(testVo.getIs_hide());
				listdatachild.add(testVo.getOrg_status());
				listdatachild.add(testVo.getCreate_by());
				listdatachild.add(testVo.getCreate_at());
				listdatachild.add(testVo.getUpdate_by());
				listdatachild.add(testVo.getUpdate_at());
				
				listdatachild.add(testVo.getMobile());
				listdatachild.add(testVo.getAddress());
				listdatachild.add(testVo.getBanknumber());
				listdatachild.add(testVo.getBanktype());
				listdatachild.add(testVo.getMembertype());
				listdatachild.add(testVo.getProduct());
				
				}
				listdata.add(listdatachild);
			}
			
			
			CreateEmailInfoUtil.createEmailInfo("C:\\2013VW\\test2013.xls", listdata);

			
			System.out.println("*********getAllReport end***********");*/
            
        } catch (OTSException e) {
            this.addActionError(e.getMessage()); 
            return ERROR;
        }
        return OPE_INFO;
    }
    
    private A_ORG_BONUSVO reportmodel2 = new A_ORG_BONUSVO();
    
    public String backup() throws Exception
    {
    	System.out.println("*********getAllReport begin***********");
		rows = a_ORGService.queryA_ORGList(reportmodel1);

		System.out.println("rows size:"+rows.size());
		
		A_ORGVO testVo;
		
		List listdata = new ArrayList();
		
		List listdatachild;
		
		List listdatachild1 = new ArrayList();
		
		listdatachild1.add("Org_id");
		listdatachild1.add("Org_code");
		listdatachild1.add("Org_name");
		listdatachild1.add("Upper_org_code");
		listdatachild1.add("Is_leaf");
		listdatachild1.add("Org_desc");
		listdatachild1.add("Org_order");
		listdatachild1.add("Is_hide");
		listdatachild1.add("Org_status");
		listdatachild1.add("Create_by");
		listdatachild1.add("Create_at");
		listdatachild1.add("Update_by");
		listdatachild1.add("Update_at");
		
		listdatachild1.add("Mobile");
		listdatachild1.add("Address");
		listdatachild1.add("Banknumber");
		listdatachild1.add("Banktype");
		listdatachild1.add("Membertype");
		listdatachild1.add("Product");
		
		listdata.add(listdatachild1);
		
		for(int j=0;j<rows.size();j++)
		{   
			testVo = new A_ORGVO();
			
			listdatachild = new ArrayList();
			
			testVo = (A_ORGVO) rows.get(j);
			 
		 
			listdatachild.add(testVo.getOrg_id());
			listdatachild.add(testVo.getOrg_code());
			listdatachild.add(testVo.getOrg_name());
			listdatachild.add(testVo.getUpper_org_code());
			listdatachild.add(testVo.getIs_leaf());
			listdatachild.add(testVo.getOrg_desc());
			listdatachild.add(testVo.getOrg_order());
			listdatachild.add(testVo.getIs_hide());
			listdatachild.add(testVo.getOrg_status());
			listdatachild.add(testVo.getCreate_by());
			listdatachild.add(testVo.getCreate_at());
			listdatachild.add(testVo.getUpdate_by());
			listdatachild.add(testVo.getUpdate_at());
			
			listdatachild.add(testVo.getMobile());
			listdatachild.add(testVo.getAddress());
			listdatachild.add(testVo.getBanknumber());
			listdatachild.add(testVo.getBanktype());
			listdatachild.add(testVo.getMembertype());
			listdatachild.add(testVo.getProduct());
			
		 
			listdata.add(listdatachild);
		}
		
		
		
		
		
		
		
		rows = a_ORG_BONUSService.queryA_ORGList(reportmodel2);

		System.out.println(" BONUS rows size:"+rows.size());
		
		A_ORG_BONUSVO testVo1;
		
		List listdata1 = new ArrayList();
		
		List listdatachild2;
		
		List listdatachild3 = new ArrayList();
		
		listdatachild3.add("org_code");
		listdatachild3.add("remaining_bonus");
		listdatachild3.add("totle_bonus");
		listdatachild3.add("extraction_time");
		listdatachild3.add("last_extraction_data");
		listdatachild3.add("last_extraction_bonus");
		listdatachild3.add("ifclear");
		listdatachild3.add("org_name");
		listdatachild3.add("mobile");
		
		listdata1.add(listdatachild3);
		
		for(int j=0;j<rows.size();j++)
		{   
			testVo1 = new A_ORG_BONUSVO();
			
			listdatachild2 = new ArrayList();
			
			testVo1 = (A_ORG_BONUSVO) rows.get(j);

			listdatachild2.add(testVo1.getOrg_code());
			listdatachild2.add(testVo1.getRemaining_bonus());
			listdatachild2.add(testVo1.getTotle_bonus());
			listdatachild2.add(testVo1.getExtraction_time());
			listdatachild2.add(testVo1.getLast_extraction_data());
			listdatachild2.add(testVo1.getLast_extraction_bonus());
			listdatachild2.add(testVo1.getIfclear());
			
			listdatachild2.add(testVo1.getOrg_name());
			listdatachild2.add(testVo1.getMobile());
 
			 
			listdata1.add(listdatachild2);
		}
		
		
		
		
		
		
		
		/*CreateEmailInfoUtil.createEmailInfo("C:\\2013VW\\test2013.xls", listdata);*/

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
		
		System.out.println("获得当前系统日期："+df.format(new Date()));// new Date()为获取当前系统时间
		
		CreateEmailInfoUtil.createEmailInfo("D:\\apache-tomcat-6.0.26\\apache-tomcat-6.0.26_2\\webapps\\cfjjc\\jsp\\usraccount\\a_html_view\\databackup"+df.format(new Date())+".xls", listdata,listdata1);
		
		
		
		
		System.out.println("*********getAllReport end***********");
		
		this.setBackupMessage(1);
		
		return "backup";
        
    }
    
    

    // 显示修改页面
    public String showEdit() throws Exception {
       try {
       		if (StringUtil.isEmpty(model.getKey())) {
				return INPUT;
			}

        	A_ORGVO param=new A_ORGVO();
        	param.setKey(model.getKey());
            model=a_ORGService.queryA_ORG(param);
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
			return ERROR_PAGE;
        }

        setOperationType(SystemConst.OPERATION_TYPE_MODIFY);
        return INPUT;
    }
    
   
    
    // 显示修改页面
    public String showEdit2() throws Exception {
       try {
       		if (StringUtil.isEmpty(model.getKey())) {
				return INPUT;
			}

        	A_ORGVO param=new A_ORGVO();
        	param.setKey(model.getKey());
            model=a_ORGService.queryA_ORG(param);
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
			return ERROR_PAGE;
        }

        setOperationType(SystemConst.OPERATION_TYPE_MODIFY);
        return "input2";
    }
    
    
    private A_ORG_BONUSVO orgbonus ;

    // 保存对机构信息表的修改
    public String doUpdate() throws Exception {
        try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setUpdate_by(usr.getUsr_id());
        	System.out.println("*************用户修改："+model.getOrg_code()+"**************");
            a_ORGService.doUpdateA_ORG(model);
            
            orgbonus = new A_ORG_BONUSVO();
            
            orgbonus.setOrg_code(model.getOrg_code());
            orgbonus.setOrg_name(model.getOrg_name());
            orgbonus.setMobile(model.getMobile());
            
            a_ORG_BONUSService.doUpdateA_ORG(orgbonus);
            
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
            return ERROR;
        }
        return OPE_INFO;
    }
    
    public String doUpdate2() throws Exception {
        try {
        	A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
        	model.setUpdate_by(usr.getUsr_id());
            a_ORGService.doUpdateA_ORG(model);
            
            orgbonus = new A_ORG_BONUSVO();
            
            orgbonus.setOrg_code(model.getOrg_code());
            orgbonus.setOrg_name(model.getOrg_name());
            orgbonus.setMobile(model.getMobile());
            
            a_ORG_BONUSService.doUpdateA_ORG(orgbonus);
            
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
            return ERROR;
        }
        return OPE_INFO;
    }

    // 删除机构信息表。允许批量删除。允许部分删除成功
    public String doDelete() throws Exception {
        try {
            for (int n = 0; n < id.length; n++) {
                A_ORGVO param = new A_ORGVO();
                param.setKey(id[n]);
                a_ORGService.doDeleteA_ORG(param);
            }
        } catch (OTSException e) {
            this.addActionError(e.getMessage());
            return ERROR;
        }
        return NONE;
    }    

    /** 以下为set,get方法 */
    public Object getModel() {
        return model;
    }
    
	public TreeConfig getTree() {
		return tree;
	}
        
        
   private List<A_ORGVO> getAllORG() {
        try {
            A_ORGVO model = new A_ORGVO();
            rows = a_ORGService.queryA_ORGList(model);
            return rows;
        } catch (OTSException ex) {
            java.util.logging.Logger.getLogger(A_ORG_BONUSAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    private void dealTreeORG(A_ORGVO a) {
        
                       r_List.clear();
        
                       getAllORG();
                       recursionFn(rows, a);
                       r_List.remove(r_List.size()-1);
                      System.out.println("团队成员数量为-----------"+r_List.size()+"----------------------");

               for(int i=0;i<r_List.size();i++)
               {
                    A_ORGVO node =(A_ORGVO)r_List.get(i);
                     System.out.println("遍历树得到的-----------------"+node.getOrg_code()+"--------------");
                     r_RList.clear();
                    recursionRFn(rows,node);
                    int level_remumber =r_RList.size();
                    int gold=0;
                    List goldlist= new ArrayList();
                    if(level_remumber>0&&level_remumber<=7)
                    {
                         node.setMembertype("1");
                       //业务员1-7套
                        System.out.println("业务员------"+node.getOrg_code()+"--------------"); 
                    }
                    if(level_remumber>7&&level_remumber<=39)
                    {
                          node.setMembertype("2");
                        //业务主管 8-39套
                          System.out.println("业务主管------"+node.getOrg_code()+"--------------"); 
                    }
/*                       if(level_remumber>19&&level_remumber<=99)
                    {
                          node.setMembertype("3");
                        //业务主管 7-39套
                          System.out.println("业务主管------"+node.getOrg_code()+"--------------"); 
                    }*/
                      if(level_remumber>39)
                    {
                          node.setMembertype("3");
                        //经理 40以上套
                         
                          gold++;
                          goldlist.add(node);
                          if(gold==4)
                          {
                              //处理退出情况
     /*                   	  node.setOrg_status("02");
                        	  node.setMembertype("9");
                         
                              try {
                             
                                  a_ORGService.doUpdateA_ORG(node);
                              } catch (OTSException ex) {
                                  java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
                              }*/
                        	  
//                        	  node.setOrg_status("02");
//                        	  node.setMembertype("9");
                          }
 
                    }
                     
                           try {
                               a_ORGService.doUpdateA_ORG(node);
                           } catch (OTSException ex) {
                               java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
                           }
                           
                           
                           
    
                
                   
               }
               
            

    }

    // 判断是否有子节点
    private boolean hasChild(List list, A_ORGVO node) {
        return getChildList(list, node).size() > 0 ? true : false;
    }
    
     private boolean hasRChild(List list, A_ORGVO node) {
        return getRChildList(list, node).size() > 0 ? true : false;
    }
// 得到子节点列表

    private List getChildList(List list, A_ORGVO node) {
        List nodeList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            A_ORGVO n = (A_ORGVO) it.next();
            if (n.getOrg_code().equals(node.getUpper_org_code())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }
    
      private List getRChildList(List list, A_ORGVO node) {
        List nodeList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            A_ORGVO n = (A_ORGVO) it.next();
            if (n.getUpper_org_code().equals(node.getOrg_code())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    } 
      private void recursionFn(List list, A_ORGVO node) {
                List childList = getChildList(list, node);// 得到子节点列表
                if (hasChild(list, node)) {// 判断是否有子节点
                        r_List.add(node);
                        Iterator it = childList.iterator();
                        while (it.hasNext()) {
                                A_ORGVO n = (A_ORGVO) it.next();
                                recursionFn(list, n);
                        }
                } else {
                        r_List.add(node);
                }
        }
    
    private void recursionRFn(List list, A_ORGVO node) {
                List childList = getRChildList(list, node);// 得到子节点列表
                if (hasRChild(list, node)) {// 判断是否有子节点
                        r_RList.add(node);
                        Iterator it = childList.iterator();
                        while (it.hasNext()) {
                                A_ORGVO n = (A_ORGVO) it.next();
                                recursionRFn(list, n);
                        }
                } else {
                        r_RList.add(node);
                }
        }
  private void dealScore()
  {
      List scoreList= r_List;

 //      <-------------------直接奖 ---------------->     

       String flag_A="";//直接积分分配标志
       String flag_B="";//间接积分分配标志
        List type1=new ArrayList();
         List type2=new ArrayList();
          List type3=new ArrayList();
   //        List type4=new ArrayList();
           
               for(int k=0;k<scoreList.size();k++)
         {
          
             A_ORGVO bb = (A_ORGVO) scoreList.get(k);
              String level = bb.getMembertype();//推荐人级别
             if (level.equals("1")) {
                 type1.add(bb);
             } else if (level.equals("2")) {
                 type2.add(bb);
             } else if (level.equals("3")) {
                 type3.add(bb);
             } /*else if (level.equals("4")) {
                 type4.add(bb);
                 
             }*/
           
         }
/*             if (type4.size() == 4) {
                  //金级会员断开

                 A_ORGVO tc = (A_ORGVO) type4.get(3);
                 dealglodgo(tc);
                 //重新计算树和积分情况
                 dealTreeORG(model);
                 dealScore();
                 return;*/
               
               if (type3.size() == 4) {
                   //金级会员断开

            
                   A_ORGVO aa = (A_ORGVO) type3.get(2);
                  dealglodgo(aa);
                  //重新计算树和积分情况
                  dealTreeORG(model);
                  dealScore();
                  return;

      }
         for(int k=0;k<scoreList.size();k++)
         {

             A_ORGVO bb = (A_ORGVO) scoreList.get(k);
              String level = bb.getMembertype();//推荐人级别
 
          if (k == 0) {
              continue;
          }
          if (k == 1) {
             
              double AA = 0;
/*              if (level.equals("1")) {
                  AA = A * a;
                  flag_A="1";
                  System.out.println("业务员直接奖");
                  //业务员直接奖
              } else */
            	  
            	  if (level.equals("1")) {
                  AA = A * b;
                  flag_A="2";
                   System.out.println("业务员直接奖");
                  //高级业务员直接奖
              } else if (level.equals("2")) {
                  AA = A * c;
                  flag_A="3";
                  System.out.println("业务主管直接奖");
                  //业务主管直接奖
              } else if (level.equals("3")) {
                  AA = A * d;
                  flag_A="4";
                  System.out.println("经理直接奖");
                  //经理直接奖
              }
              //更新直接奖
              
                A_ORG_BONUSVO aob = new A_ORG_BONUSVO();
                aob.setOrg_code(bb.getOrg_code());
                A_ORG_BONUSVO temp ;
                 try {
         
                     temp = a_ORG_BONUSService.queryA_ORG(aob);
                     temp.setBankname(bb.getBanktype());
                     temp.setBanknumber(bb.getBanknumber());
                     temp.setMobile(bb.getMobile());
                     update_JJ(AA, temp);
                 } catch (OTSException ex) {
                     java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
                 }
              
           
             continue;
          }
       //      <-------------------间接奖奖 ---------------->  
/*          if(flag_A.equals("1"))
          {
             if(bb.getMembertype().equals("2"))
             {
                  flag_A="2";
                  System.out.println("高级业务员间接奖");
                   double jj=  A*e;
                A_ORG_BONUSVO aob = new A_ORG_BONUSVO();
                aob.setOrg_code(bb.getOrg_code());
                A_ORG_BONUSVO temp;
                 try {
                     temp = a_ORG_BONUSService.queryA_ORG(aob); 
                       update_JJ(jj,temp);
                 } catch (OTSException ex) {
                     java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
                 }
              
                
                 
             }
          }
          else  */
        	  
        	  
        	  if(flag_A.equals("2"))
          {
             if(bb.getMembertype().equals("2"))
             {
                  flag_A="3";
                   System.out.println("业务主管间接奖");
                    double jj=  A*f;
                      A_ORG_BONUSVO aob = new A_ORG_BONUSVO();
                aob.setOrg_code(bb.getOrg_code());
                A_ORG_BONUSVO temp;
                 try {
                     
                     temp = a_ORG_BONUSService.queryA_ORG(aob); 
                        temp.setBankname(bb.getBanktype());
                  temp.setBanknumber(bb.getBanknumber());
                  temp.setMobile(bb.getMobile());
                       update_JJ(jj,temp);
                 } catch (OTSException ex) {
                     java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
                 }
             }
             
          }
          else if(flag_A.equals("3"))
          {
              if(bb.getMembertype().equals("3"))
              {
                    System.out.println("经理间接奖");
                      double jj=  A*g;
                      A_ORG_BONUSVO aob = new A_ORG_BONUSVO();
                aob.setOrg_code(bb.getOrg_code());
                A_ORG_BONUSVO temp;
                 try {
                     temp = a_ORG_BONUSService.queryA_ORG(aob); 
                     temp.setBankname(bb.getBanktype());
                     temp.setBanknumber(bb.getBanknumber());
                     temp.setMobile(bb.getMobile());
                       update_JJ(jj,temp);
                 } catch (OTSException ex) {
                     java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
                 }
              }
          }

      }
//      <-------------------间接奖奖 ---------------->        
 //      <-------------------补助奖 ---------------->  
         
         
         
/*      if(type3.size()>0&&type2.size()>1)
      {
          //银级补助 减去4%
          double yjbz = A * k;
          A_ORGVO fj = (A_ORGVO) type3.get(0);
          A_ORG_BONUSVO aob = new A_ORG_BONUSVO();
          aob.setOrg_code(fj.getOrg_code());
          A_ORG_BONUSVO temp;
          try {
              temp = a_ORG_BONUSService.queryA_ORG(aob);
              update_JJ(-yjbz, temp);
          } catch (OTSException ex) {
              java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
          }
       //银级补助  减去4%
          //增加4% 
          A_ORGVO bjt = (A_ORGVO) type2.get(type2.size() - 1);
          A_ORG_BONUSVO aob1 = new A_ORG_BONUSVO();
          aob.setOrg_code(bjt.getOrg_code());
          A_ORG_BONUSVO temp1;
          try {
              temp1 = a_ORG_BONUSService.queryA_ORG(aob);
              update_JJ(yjbz, temp1);
          } catch (OTSException ex) {
              java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
          }
           //增加4% 
      }
      //补助 金级
      if(type4.size()>0&&type3.size()>0)     
      {
          double p=A*l;
            A_ORGVO b = (A_ORGVO) type4.get(0);
              A_ORG_BONUSVO aob1 = new A_ORG_BONUSVO();
          aob1.setOrg_code(b.getOrg_code());
          A_ORG_BONUSVO temp1;
          try {
              temp1 = a_ORG_BONUSService.queryA_ORG(aob1);
              update_JJ(-p, temp1);
          } catch (OTSException ex) {
              java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
          }
          if(type3.size()>2)
          {
              double q=A*m;
              A_ORGVO nn = (A_ORGVO) type3.get(type3.size() - 1);
              A_ORG_BONUSVO aob2 = new A_ORG_BONUSVO();
              aob2.setOrg_code(nn.getOrg_code());
              A_ORG_BONUSVO temp2;
              try {
                  temp1 = a_ORG_BONUSService.queryA_ORG(aob1);
                  update_JJ(q, temp1);
              } catch (OTSException ex) {
                  java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
              }
              
              double z=A*n;
              A_ORGVO bb = (A_ORGVO) type3.get(type3.size() -2);
              A_ORG_BONUSVO ao3 = new A_ORG_BONUSVO();
              ao3.setOrg_code(bb.getOrg_code());
           
              try {
                  temp1 = a_ORG_BONUSService.queryA_ORG(ao3);
                  update_JJ(z, temp1);
              } catch (OTSException ex) {
                  java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex); 
              
          }
      }
          if(type3.size()==2)
          {
               double q=A*l;
              A_ORGVO nn = (A_ORGVO) type3.get(type3.size() - 1);
              A_ORG_BONUSVO aob2 = new A_ORG_BONUSVO();
              aob2.setOrg_code(nn.getOrg_code());
              A_ORG_BONUSVO temp2;
              try {
                  temp2 = a_ORG_BONUSService.queryA_ORG(aob1);
                  update_JJ(q, temp2);
              } catch (OTSException ex) {
                  java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
              }
               
          }
      }*/
         
      
      
      
      
         
//      <-------------------补助奖 ---------------->     
      
  //      <-------------------分红奖 ---------------->  
      
      if(type3.size()>0)
      {
          double fh=A*p;
          for(int i=0;i<type3.size();i++)
          {
            A_ORGVO a=  (A_ORGVO) type3.get(i);
              A_ORG_BONUSVO aob2 = new A_ORG_BONUSVO();
              aob2.setOrg_code(a.getOrg_code());
              A_ORG_BONUSVO temp2;
              try {
                  temp2 = a_ORG_BONUSService.queryA_ORG(aob2);
                  temp2.setBankname(a.getBanktype());
                  temp2.setBanknumber(a.getBanknumber());
                  temp2.setMobile(a.getMobile());
                  update_JJ(fh, temp2);
              } catch (OTSException ex) {
                  java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
              }
            
          }
      }
      
      
      //      <-------------------分红奖 ---------------->     
      
  }
  
  
  private void update_JJ(double AA,A_ORG_BONUSVO temp)
  {
            try {

                String remain = temp.getRemaining_bonus();
                double totle_remain =0;
                if(remain==null||remain.equals("0"))
                {
                    totle_remain = AA;
                }
                else
                {
                    totle_remain = Double.parseDouble(remain) + AA;
                }
                String tote_bonuss= temp.getTotle_bonus();
                double tote_bonus =0;
                if(tote_bonuss==null||tote_bonuss.equals("0"))
                {
                    tote_bonus = AA;
                }
                else
                {
                    tote_bonus= AA + Double.parseDouble(temp.getTotle_bonus());
                }
                
                temp.setRemaining_bonus(totle_remain + "");
                temp.setTotle_bonus(tote_bonus + "");
//                temp.setLast_extraction_bonus(AA+"");
//                temp.setExtraction_time(DateTimeUtil.getDateTime());
//           
                temp.setIfclear("1");
                a_ORG_BONUSService.doUpdateA_ORG(temp);
            } catch (OTSException ex) {
                java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
            }
  }
  
  private void dealglodgo(A_ORGVO a)
  {
//      a.setOrg_status("02");
//      a.setMembertype("9");
      a.setUpper_org_code("1002");
      try {
          a_ORGService.doUpdateA_ORG(a);
      } catch (OTSException ex) {
          java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
      }
//    A_ORGVO b= new A_ORGVO();
//    b.setUpper_org_code(a.getOrg_code());
//      try {
//         List bs= a_ORGService.queryA_ORGList(b);
//         for(int i=0;i<bs.size();i++)
//         {
//          A_ORGVO bbb=   (A_ORGVO) bs.get(i);
//          bbb.setUpper_org_code("1002");
//          a_ORGService.doUpdateA_ORG(bbb);
//         }
//      } catch (OTSException ex) {
//          java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
//      }
      
  }
  
  
  private void clearALLRemaining()
  {
            try {
                A_ORG_BONUSVO a= new A_ORG_BONUSVO();
              List<A_ORG_BONUSVO> bbs=  a_ORG_BONUSService.queryA_ORGList(a);
             for(A_ORG_BONUSVO b:bbs)
             {
                 b.setRemaining_bonus("0");
                 a_ORG_BONUSService.doUpdateA_ORG(b);
             }
            } catch (OTSException ex) {
                java.util.logging.Logger.getLogger(A_ORGAction.class.getName()).log(Level.SEVERE, null, ex);
            }
  }
}
